using System;

namespace csammisrun.OscarLib.Utility
{
	/// <summary>
	/// Provides static methods for processing messages in SNAC family 0x0008 -- popups service
	/// </summary>
	internal static class SNAC08
	{
    /// <summary>
		/// Processes a popup message sent by the server -- SNAC(08,02)
		/// </summary>
		/// <param name="dp">A <see cref="DataPacket"/> object with a buffer containing SNAC(08,02)</param>
		public static void ProcessPopupMessage(DataPacket dp)
		{
			byte[] SNACData = dp.Data;

			int index = 0;
			ushort key, keylength;
			ushort width = 0, height = 0, delay = 0;
			string url = "", message = "";
			while(index < SNACData.Length)
			{
				key = Marshal.ByteArrayToUshort(SNACData, ref index);
				keylength = Marshal.ByteArrayToUshort(SNACData, ref index);
				switch(key)
				{
					case 0x0001:
						message = Marshal.ByteArrayToString(SNACData, ref index, keylength, Marshal.ASCII);
						break;
					case 0x0002:
						url = Marshal.ByteArrayToString(SNACData, ref index, keylength, Marshal.ASCII);
						break;
					case 0x0003:
						width = Marshal.ByteArrayToUshort(SNACData, ref index);
						break;
					case 0x0004:
						height = Marshal.ByteArrayToUshort(SNACData, ref index);
						break;
					case 0x0005:
						delay = Marshal.ByteArrayToUshort(SNACData, ref index);
						break;
					default:
						index += keylength;
						break;
				}
			}

			dp.ParentSession.OnPopupMessage(width, height, delay, url, message);
		}
	}
}
